package com.pony.result;

import com.pony.enums.AppHttpCodeEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    private Boolean success; //是否成功
    private Integer code; //状态码
    private String message; //返回信息
    private Map<String, Object> data = new HashMap<String, Object>(); //返回的数据
//    private List<Menu> dataList = new ArrayList<>();

    //将构造方法私有化，这样别人就不能 new 了，必须使用我们提供的方法
    private R() {
    }

    //成功方法
    public static R success() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("成功");
        return r;
    }

    //成功方法
    public static R success(AppHttpCodeEnum codeEnum) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(codeEnum.getCode());
        r.setMessage(codeEnum.getMessage());
        return r;
    }

    //成功方法
    public static R success(AppHttpCodeEnum codeEnum, String message) {
        R r = new R();
        r.setSuccess(true);
        r.setCode(codeEnum.getCode());
        r.setMessage(message);
        return r;
    }

    //失败方法
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(400);
        r.setMessage("失败");
        return r;
    }

    //失败方法
    public static R error(AppHttpCodeEnum codeEnum) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(codeEnum.getCode());
        r.setMessage(codeEnum.getMessage());
        return r;
    }

    //失败方法
    public static R error(AppHttpCodeEnum codeEnum, String message) {
        R r = new R();
        r.setSuccess(false);
        r.setCode(codeEnum.getCode());
        r.setMessage(message);
        return r;
    }

    //方便链式编程，修改 返回数据
    // r.success().isSuccess(true).code(200)
    // r.success() 会返回一个对象，isSuccess(true)返回 this，谁调用这个 this 即使谁
    public R isSuccess(Boolean success) {
        this.success = success;
        return this;
    }

//    public R code(Integer code) {
//        this.code = code;
//        return this;
//    }

    public R message(String message) {
        this.message = message;
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

//    public R data(ArrayList<Menu> list){
//        this.setDataList(list);
//        return this;
//    }
}